<script setup lang="ts">
  import { Field } from 'vee-validate';
  import { useLocale } from '@/locales/useLocale';
  const { t } = useLocale();
  const props = withDefaults(
    defineProps<{
      name: string;
      placeholder?: string;
      label?: string;
      captchaUrl: Recordable;
      requiredField?: boolean;
      size?: 'large' | 'small' | 'default';
      animateClass?: string;
    }>(),
    {
      name: 'default',
      label: '',
      placeholder: '',
      //@ts-ignore
      captchaUrl: {} as Recordable,
      requiredField: false,
      size: 'default',
      animateClass: '',
    }
  );
  const emits = defineEmits(['imgClick']);
  const handleImageClick = () => {
    emits('imgClick');
  };
</script>

<template>
  <Field :name="name" v-slot="{ value, field, errorMessage }">
    <el-form-item
      :error="errorMessage"
      :label="label && t(label)"
      :class="{ 'field-required': requiredField, [animateClass]: animateClass }"
      :size="size"
    >
      <div class="flex justify-between items-center w-full">
        <el-input
          class="!w-[65%]"
          :placeholder="placeholder && t(placeholder)"
          :model-value="value"
          v-bind="field"
        />
        <el-image
          v-if="captchaUrl.value"
          :src="captchaUrl.value"
          @click="handleImageClick"
          class="w-[30%] h-8 rounded shadow cursor-pointer"
          alt="验证码"
        />
      </div>
    </el-form-item>
  </Field>
</template>

<style lang="scss"></style>
